package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        //链接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的链接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
            boolean isRight = false;
        try {
           isRight = guestbookDao.addMessage(guestbook);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> querryInfo() {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(connection);
       List<Object> list =null;
        try {
           list = guestbookDao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteBean(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        //链接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的链接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.deleteById(id);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public Guestbook updateBean(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection connection = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(connection);
        List<Object> list =null;
        Guestbook gb = guestbookDao.updateById(id);
        return gb;
    }

    @Override
    public boolean updateByObj(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        //链接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的链接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.updateByObj(gb);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        DBManager dbManager = DBManager.getInstance();
        //链接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的链接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        Pagination pagination =null;
        try {
          pagination=  guestbookDao.getGuestbookByPage(pageSize,pageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return pagination;
    }
}
